package com.tianji.learning.mq;

import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.utils.StringUtils;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE;
import static com.tianji.common.constants.MqConstants.Queue.REPLY_LIKE_CHANGE_QUEUE;

/**
 * @Author: 19311
 * @CreateTime: 2025-03-27
 * @Description: 点赞数量变动监听
 * @Version: 1.0
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LikeChangeListener {

    private final IInteractionReplyService interactionReplyService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = REPLY_LIKE_CHANGE_QUEUE, durable = "true"),
            exchange = @Exchange(name = LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = "reply"
    ))
    @Transactional
    public void likeChange(List<LikedTimesDTO> likedTimesDTOs) {
        log.error("收到点赞数量变更通知");

        for (LikedTimesDTO likedTimesDTO : likedTimesDTOs) {
            interactionReplyService.lambdaUpdate()
                    .eq(InteractionReply::getId, likedTimesDTO.getBizId())
                    .set(InteractionReply::getLikedTimes, likedTimesDTO.getLikedTimes())
                    .update();
        }

    }
}
